package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */


class WineVarietalManagement {
    
    def listWineVarietals() {
        def wineVarietals = WineVarietal.list()
        JSONArray arr = new JSONArray()
        wineVarietals.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("wineVarietalName",it.wineVarietalName)
            obj.put("description",it.description)
            obj.put("wineType", it.wineType)
//            println "WT : " + it.wineType
//            obj.put("wineType",it.WineTypes.wineTypeName)
            arr.add(obj)
        }
        
        return arr
    }
    
    def getWineVarietalByID(id) {
        def wv = WineVarietal.findById(id)
        JSONObject obj = new JSONObject()
        obj.put("id",wv.id)
        obj.put("wineVarietalName",wv.wineVarietalName)
        obj.put("description",wv.description)
        obj.put("wineType", wv.wineType)
        
        return obj
    }
    
    def getWineVarietalForDbByWineVarietalName(wineVarietalName) {
        def wv = WineVarietal.findByWineVarietalName(wineVarietalName)
        
        return wv
    }
    
    def addWineVarietal(o) {
        def status
        
//        WineVarietal wv = new WineVarietal()
        def wineVarietalName = o.get("wineVarietalName")
        def description = ""
        if (o.get("description") == "") {
            description = "-"
        }
        else {
            description = o.get("description")
        }
        def wineType = o.get("wineType")
//        def wt = WineType.findByTypeName(o.get("wineType"))
//        wv.wineType = wt
        WineTypeManagement wtman = new WineTypeManagement()
        WineType wt = wtman.getWineTypeForDbByWineTypeName(o.get("wineType"))
            
        wt.addToWineVarietals(new WineVarietal(wineVarietalName:wineVarietalName,description:description))
        
        if (wt.save()) {
            status = true
        }
        else {
            status = false
        }        
        println 'WineVarietalManagement >> Add Wine Var >> ' + status
        
        return status
    }
    
    def updateWineVarietal(wineVarietal) {
        def status
        
        WineVarietal wv = WineVarietal.findById(wineVarietal.id)
        if (wineVarietal.has("wineVarietalName")) { wv.wineVarietalName = wineVarietal.wineVarietalName }
        if (wineVarietal.has("description")) { wv.description = wineVarietal.description }
        if (wineVarietal.has("wineType")) { 
            WineTypeManagement wtman = new WineTypeManagement()
//            println "WINE TYPE: " + wineVarietal.wineType
            WineType wt = wtman.getWineTypeForDbByWineTypeName(wineVarietal.wineType)
//            println "Search for WIneType: " + wt
//            println "wine type before update: " + wv.wineType
            wv.wineType = wt
//            println "wine type after update: " + wv.wineType
        }
        if (wv.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'WineVarietalManagement >> Update Wine >> ' + status
        
        return status
    }
    
    def deleteWineVarietal(id) {
        def status
        
        WineVarietal wv = WineVarietal.findById(id)
        if (wv.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'WineVarietalManagement >> Delete Wine >> ' + status
        
        return status
    }
    
}

